package films;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class PostAction extends ActionSupport {

	private static FilmsCollection collection = new FilmsCollection();
	private String title,opinion, rate, oldTitle;
	private String ruta = "pelis.dat";
	
	
	/**
	 * put a new film in the collection and save the state of collection
	 */
	public String execute(){
		if (!validateFields()) return INPUT;
		else{
			Film f = new Film(title,rate,opinion);
			collection.put(f);
			grabar(collection);
			return SUCCESS;
		}
	}
	/**
	 * edits some parameters of a specific film and saves the state of collection
	 * @return success
	 */
	public String edit(){
		if (!validateFields()) return INPUT;
		else{			
			collection.remove(oldTitle);
			Film f = new Film(title,rate,opinion);
			collection.put(f);
			grabar(collection);
			return SUCCESS;
		}
	}
	
	/**
	 * remove a specific film of the collection and saves its state
	 * @return success
	 */
	public String remove(){
		collection.remove(title);
		grabar(collection);
		return SUCCESS;
	}
	
	/**
	 * checks if parameters entered by the user are valid 
	 * @return true if they are, false otherwise
	 */
	private boolean validateFields(){
		boolean toReturn = true;
		if (getTitle().length() == 0){
            addFieldError("title", "Title is required");
            return toReturn = false; 
		}
        if (!validRate()){
            addFieldError("rate", getText("Rate must be an integer between [1,10]"));
            return toReturn = false;
        }
        if (getOpinion().length() == 0){
        	addFieldError("opinion", "Opinion is required");
        	return toReturn = false;
        }
        return toReturn;
	}
	
	/**
	 * checks if the rate is valid
	 * @return true if it is, false if not
	 */
	private boolean validRate(){
		return (getRate().equals("1") || getRate().equals("2") || getRate().equals("3") || getRate().equals("4") || getRate().equals("5") || getRate().equals("6") || getRate().equals("7") || getRate().equals("8") || getRate().equals("9") || getRate().equals("10")); 
	}
	
	/**
	 * loads collection
	 * @return success
	 */
	public String wakeUpAction(){
		if (recuperar() != null) collection = recuperar();
		return SUCCESS;
	}
	
	/**
	 * tries to load collection from disk
	 * @return the collection
	 */
	public FilmsCollection recuperar(){
		FilmsCollection films=null;
		try {
			// Abrir archivo y recuperar mapeo:
			File file = new File( ruta );
			ObjectInputStream in = new ObjectInputStream(new FileInputStream( ruta ));
			films=(FilmsCollection) in.readObject();
			in.close();			
		} catch (FileNotFoundException e) {
			return null;
			//e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return films;
	}
	
	/**
	 * tries to save the collection to disk
	 * @param films, collection to save
	 */
	public void grabar(FilmsCollection films){
		try {
			File file =new File(ruta);
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(ruta));
			out.writeObject( films );
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	///////////////////GETTERS & SETTERS///////////////////////////
	
	public void setTitle(String title){
		this.title = title;
	}
	
	public void setOpinion(String opinion){
		this.opinion = opinion;
	}
	
	public void setRate(String rate){
		this.rate = rate;
	}
	
	public String getTitle(){
		return title;
	}
	
	public String getOpinion(){
		return opinion;
	}
	
	public String getRate(){
		return rate;
	}
	
	public void setOldTitle(String oldTitle){
		this.oldTitle = oldTitle;
	}
	
	public String getOldTitle(){
		return oldTitle;
	}
	
	public void setCollection(FilmsCollection collection){
		PostAction.collection = collection;
	}
	
	public FilmsCollection getCollection(){
		return collection;
	}
	
	
}
